from django.shortcuts import render
from course import models,serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
# Create your views here.


class HomeCateView(APIView):
    def get(self,request):
        catelist = models.CategoryModel.objects.filter(recommend=True,parent=0).order_by("order")
        ser = serializers.CategorySer(catelist,many=True)
        data = ser.data
        for i in data:
            i['son'] = i['son'][0:3]
        return Response({"msg":"ok","data":ser.data})


class CourseView(APIView):
    def get(self,request):
        clist = models.CategoryModel.objects.filter(id=request.query_params.get('cid')).first()
        couser = serializers.CategorySer(clist)
        print(couser.data['son'])
        sonlist = []
        for i in couser.data['son']:
            sonlist.append(i['id'])
        homecourse = models.CourseModel.objects.filter(category__in=sonlist).order_by("-id")[0:4]
        coursedata = serializers.CourseSer(homecourse,many=True).data
        datalist = couser.data
        datalist['course'] = coursedata
        return Response({"msg":"ok","data":datalist})


class CateParent(APIView):
    def get(self,request):
        cate = models.CategoryModel.objects.filter(parent=0)
        ser = serializers.CateSer(cate,many=True)
        return Response({"msg":"ok","data":ser.data})

class CateSon(APIView):
    def get(self,request):
        cid = int(request.query_params.get("cid"))
        if cid != 0 :
            clist = models.CategoryModel.objects.filter(parent=cid)
        else:
            clist = models.CategoryModel.objects.filter(parent_id__gt=0)
        ser = serializers.CateSer(clist,many=True)
        return Response({"msg":"ok","data":ser.data})


class CoursePage(PageNumberPagination):
    page_size = 6



class CourseCatePage(ListAPIView):
    serializer_class = serializers.CourseSer
    pagination_class = CoursePage

    def get_queryset(self):
        categoryID = int(self.kwargs.get('categoryID',0))
        directionID = int(self.kwargs.get('directionID',0))
        print(directionID)
        print(categoryID)
        queryset = models.CourseModel.objects.all()
        if categoryID == 0 and directionID != 0:
            data = models.CategoryModel.objects.filter(id=directionID).first()
            ser = serializers.CategorySer(data)
            print(ser.data)
            idlst = []
            for i in ser.data['son']:
                idlst.append(i['id'])
            print(idlst)
            queryset = models.CourseModel.objects.filter(category__in=idlst)
        if categoryID != 0 :
            queryset = models.CourseModel.objects.filter(category=categoryID)
        return queryset
        # cid = request.query_params.get("cid")
        # if cid:
        #         clist = queryset.(category=cid)
        # else:
        #     clist = models.CourseModel.objects.all()
        # ser = serializers.CourseSer(clist,many=True)
        # return Response({"msg":"ok","data": ser.data )

